﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using MN.Framework.Log;

namespace Mn.Framework.Service.Messaging
{
    public class MailSender
    {
        private ISmtpServer Server { get; set; }
        private string From { get; set; }
        private string FromTitle { get; set; }
        public MailSender(ISmtpServer smtpServer, string from)
        {
            this.Server = smtpServer;
            this.From = from;
        }
        public MailSender(ISmtpServer smtpServer, string from, string fromTitle)
        {
            this.Server = smtpServer;
            this.From = from;
            this.FromTitle = fromTitle;
        }

        public int SendManyOneByOne(List<string> emailsAndIds, string subject, string body, int sleepTimeInterval, ref string result)
        {
            int trueResult = 0;
            foreach (var email in emailsAndIds)
            {
                if (MN.Framework.Utility.Web.ValidEmail(email))
                {
                    string msg = string.Empty;
                    if (!SendTo(email, subject, body, out msg))
                        result += "," + msg;
                    else
                        trueResult++;
                }
            }
            return trueResult;
        }

        public bool SendTo(string toEmail, string subject, string body, out string result)
        {
            try
            {
                var mail = new MailMessage();
                SmtpClient smtpServer = new SmtpServerConfig(Server);
                mail.From = string.IsNullOrEmpty(FromTitle) ? new MailAddress(From) : new MailAddress(From, FromTitle);
                mail.Subject = subject;
                mail.IsBodyHtml = true;
                //mail.Priority = MailPriority.High;
                mail.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
                mail.Body = body;// string.Format(body, "email=" + email.Value + "&id=" + email.Key);
                System.Net.Mail.AlternateView plainView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(System.Text.RegularExpressions.Regex.Replace(mail.Body, @"<(.|\n)*?>", string.Empty), null, "text/plain");
                System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mail.Body, null, "text/html");
                mail.AlternateViews.Add(plainView);
                mail.AlternateViews.Add(htmlView);

                if (MN.Framework.Utility.Web.ValidEmail(toEmail))
                {
                    mail.To.Add(toEmail);
                    smtpServer.Send(mail);
                    //System.Threading.Thread.Sleep(10000);
                    result = "OK sended Email to " + toEmail;

                }
                else
                    result = "Error Email Address Invalid";

                return true;
            }
            catch (Exception ex)
            {
                result = "Error sending email to " + toEmail + ex.Message;

                return false;
            }

        }
        public bool SendBcc(List<string> bccEmails, string subject, string body, out string result)
        {
            try
            {
                var mail = new MailMessage();
                SmtpClient smtpServer = new SmtpServerConfig(Server);
                mail.From = string.IsNullOrEmpty(FromTitle) ? new MailAddress(From) : new MailAddress(From, FromTitle);
                mail.Subject = subject;
                mail.IsBodyHtml = true;
                //mail.Priority = MailPriority.High;
                mail.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
                mail.Body = body;// string.Format(body, "email=" + email.Value + "&id=" + email.Key);
                System.Net.Mail.AlternateView plainView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(System.Text.RegularExpressions.Regex.Replace(mail.Body, @"<(.|\n)*?>", string.Empty), null, "text/plain");
                System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mail.Body, null, "text/html");
                mail.AlternateViews.Add(plainView);
                mail.AlternateViews.Add(htmlView);

                foreach (var email in bccEmails)
                {
                    if (MN.Framework.Utility.Web.ValidEmail(email))
                        mail.Bcc.Add(email);
                }

                smtpServer.Send(mail);
                //System.Threading.Thread.Sleep(10000);
                result = "OK sended Email to " + mail.Bcc.Count;

                return true;
            }
            catch (Exception ex)
            {
                result = "Error sending email bcc " + ex.Message;

                return false;
            }

        }
    }

}
